﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emlyn.AudioToVideo.JobProcessorService.TestApp.JobProcessorService;
using Emlyn.AudioToVideo.JobProcessorLib.Interfaces;
using System.ServiceModel;
using Emlyn.AudioToVideo.JobProcessorLib.core;

namespace Emlyn.AudioToVideo.JobProcessorService.TestApp
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            PopulateJobTypeCombo();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            txtJobID.Text = Guid.NewGuid().ToString();

            JobProcessorService.JobProcessorSoapClient lclient = GetSoapClient();

            string lerrorMessage;

            string ljobType = (cmbJobType.SelectedItem as Type).Name;

            int resultCode = lclient.CreateJob(
                        txtJobID.Text,
                        ljobType,
                        txtJobDetails.Text,
                        String.Empty,
                        out lerrorMessage
                        );

            if (resultCode == 0)
            {
                txtOutput.Text += "Success" + Environment.NewLine;
            }
            else
            {
                txtOutput.Text += "Failed: ErrorCode=" + resultCode.ToString() + ", Message=" + lerrorMessage + Environment.NewLine;
            }
        }

        private JobProcessorService.JobProcessorSoapClient GetSoapClient()
        {
            return new JobProcessorSoapClient(new BasicHttpBinding(), new System.ServiceModel.EndpointAddress(txtUrl.Text));
        }

        private void btnGetStateAndProgress_Click(object sender, EventArgs e)
        {
            JobProcessorService.JobProcessorSoapClient lclient = GetSoapClient();
            
            string lerrorMessage;
            int ljobStateCode;
            int ljobProgress;
            string ljobErrorMessage; 

            int resultCode = lclient.GetJobStatusAndProgress(
                        txtJobID.Text,
                        out ljobStateCode,
                        out ljobErrorMessage,
                        out ljobProgress,
                        out lerrorMessage
                        );

            if (resultCode == 0)
            {
                JobState ljobState = JobState.InternalError;

                if (ljobStateCode >= 0)
                    ljobState = (JobState)ljobStateCode;

                txtOutput.Text += "State=" + ljobState.ToString() + ", Progress=" + ljobProgress.ToString() + "%" + ", JobErrorMessage=" + ljobErrorMessage + Environment.NewLine;
            }
            else
            {
                txtOutput.Text += "Failed: ErrorCode=" + resultCode.ToString() + ", Message=" + lerrorMessage + Environment.NewLine;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            JobProcessorService.JobProcessorSoapClient lclient = GetSoapClient();

            string lerrorMessage;

            int resultCode = lclient.StartJob(
                        txtJobID.Text,
                        out lerrorMessage
                        );

            if (resultCode == 0)
            {
                txtOutput.Text += "Success" + Environment.NewLine;
            }
            else
            {
                txtOutput.Text += "Failed: ErrorCode=" + resultCode.ToString() + ", Message=" + lerrorMessage + Environment.NewLine;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            JobProcessorService.JobProcessorSoapClient lclient = GetSoapClient();

            string lerrorMessage;

            int resultCode = lclient.CancelJob(
                        txtJobID.Text,
                        out lerrorMessage
                        );

            if (resultCode == 0)
            {
                txtOutput.Text += "Success" + Environment.NewLine;
            }
            else
            {
                txtOutput.Text += "Failed: ErrorCode=" + resultCode.ToString() + ", Message=" + lerrorMessage + Environment.NewLine;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            JobProcessorService.JobProcessorSoapClient lclient = GetSoapClient();

            string lerrorMessage;

            int resultCode = lclient.DeleteJob(
                        txtJobID.Text,
                        out lerrorMessage
                        );

            if (resultCode == 0)
            {
                txtOutput.Text += "Success" + Environment.NewLine;
            }
            else
            {
                txtOutput.Text += "Failed: ErrorCode=" + resultCode.ToString() + ", Message=" + lerrorMessage + Environment.NewLine;
            }
        }

        public void PopulateJobTypeCombo()
        {
            cmbJobType.Items.Clear();

            foreach (Type ljobType in wandev.Common.Reflection.Utils.GetConcreteDescendantsFromAllAssemblies(typeof(JobBase)))
            {
                cmbJobType.Items.Add(ljobType);
            }

            if (cmbJobType.Items.Count > 0)
                cmbJobType.SelectedIndex = 0;
        }
    }
}
